package com.dmcb.trade.business.crawlers.article;

import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 文艺生活文章解析
 * Created by Administrator on 2016/6/3.
 */
@Service
public class WenYiShengHuoParser  extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.WENYISHENGHUO_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "div.post-meta h1";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "span.post-date";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.post-content";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.post-content>p>a img[src]";
    }


    @Override
    protected void setPostDate(Document doc, Article article) {
        Element element = doc.select(dateSelect()).first();
        if(element != null){
            String time = text(element);
            String[] times = time.replace(" ","").split(",");
            if(times.length == 2){
                Map<String,String> mapmonth = new HashMap<String, String>();
               String[] arraymonth = new String[]{"一","二","三","四","五","六","七","八","九","十","十一","十二"};
                for(int i = 0 ;i< arraymonth.length;i++){
                    mapmonth.put(arraymonth[i],i+1+"-");
                }
                for(String key:mapmonth.keySet()){
                    if(times[0].contains(key)){
                        times[0]= times[0].replaceFirst(key, mapmonth.get(key));
                        time = times[1]+"-"+times[0];
                        Date date = DateUtil.parseDate(time);
                        if (date != null) {
                            article.setPostTime(date);
                        }
                        break;
                    }
                }
            }
        }
    }

    @Override
    protected String platform() {
        return "文艺生活";
    }
}
